package com.ql.gamelogm.service.impl;

import com.ql.gamelogm.dao.LogDao;
import com.ql.gamelogm.entities.Log;
import com.ql.gamelogm.entities.RedisCache;
import com.ql.gamelogm.entities.SearchHistory;
import com.ql.gamelogm.entities.WarnLog;
import com.ql.gamelogm.service.LogService;
import com.ql.gamelogm.utils.List2PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogDao logDao;
    @Autowired
    private List2PageUtil list2PageUtil;
    @Autowired
    private RedisCache redisCache;
    @Override
    public void savelog(Log log) { this.logDao.savelog(log); }

    @Override
    public List<Log> findAllLogs() {
        return logDao.findAllLogs();
    }

    @Override
    public void SaveSearchHistory(SearchHistory searchHistory) {
        this.logDao.SaveSearchHistory(searchHistory);
    }

    @Override
    public List<SearchHistory> findAllSearchHistory() {
        return logDao.findAllSearchHistory();
    }


    @Override
    public List<WarnLog> findAllWarnLog() { return logDao.findAllWarnLog(); }

    @Override
    public List<WarnLog> findWarnLogById(int id) {
        return logDao.findWarnLogById(id);
    }

    @Override
    public boolean saveWarnLog(WarnLog warnLog) {
        return logDao.saveWarnLog(warnLog);
    }

    @Override
    public boolean updateWaitWarnLogState(int id, WarnLog warnLog) {
        return logDao.updateWaitWarnLogState(id,warnLog);
    }

    @Override
    public boolean updateAcepWarnLogState(int id, WarnLog warnLog) {
        return logDao.updateAcepWarnLogState(id,warnLog);
    }

    @Override
    public boolean saveRecentTime(String date) {
        return logDao.saveRecentTime(date);
    }

    @Override
    public Page<Log> getLogList(int pageNum, int pageSize,String operator) {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<Log> logs=redisCache.getLogRedis();
        List<Log> matchLogList=new ArrayList<Log>();
        for(int i=0;i<logs.size();i++){
            if(logs.get(i).getOperator().equals(operator)){
                matchLogList.add(logs.get(i));
            }
        }
        return list2PageUtil.listConvertToPage(matchLogList,pageable);
    }

    @Override
    public Page<WarnLog> getWaitWarnLogList(int pageNum, int pageSize) throws ParseException {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<WarnLog> warnLogList=logDao.findAllWarnLog();
        List<WarnLog> matchLogList=new ArrayList<WarnLog>();
        for(int i=0;i<warnLogList.size();i++){
            if(warnLogList.get(i).getState().equals("待接收")){
                matchLogList.add(warnLogList.get(i));
            }
        }
        return list2PageUtil.listConvertToPage(matchLogList,pageable);
    }

    @Override
    public Page<WarnLog> getWarnLogList(int pageNum, int pageSize) throws ParseException {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<WarnLog> warnLogList=logDao.findAllWarnLog();
        List<WarnLog> matchLogList=new ArrayList<WarnLog>();
        for(int i=0;i<warnLogList.size();i++){
            if(warnLogList.get(i).getState().equals("已接收")){
                matchLogList.add(warnLogList.get(i));
            }
        }

        return list2PageUtil.listConvertToPage(matchLogList,pageable);
    }

    @Override
    public Page<WarnLog> getSolvedWarnLogList(int pageNum, int pageSize) throws ParseException {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<WarnLog> warnLogList=logDao.findAllWarnLog();
        List<WarnLog> matchLogList=new ArrayList<WarnLog>();
        for(int i=0;i<warnLogList.size();i++){
            if(warnLogList.get(i).getState().equals("已处理")){
                matchLogList.add(warnLogList.get(i));
            }
        }

        return list2PageUtil.listConvertToPage(matchLogList,pageable);
    }


    @Override
    public Page<SearchHistory> getAllSearchHisroty(int pageNum, int pageSize,String operator) throws ParseException {
        Pageable pageable=PageRequest.of(pageNum,pageSize);
        List<SearchHistory> searchHistoryList=redisCache.getSearchHistoryLogRedis();
        List<SearchHistory> searchHistoryList1=new ArrayList<SearchHistory>();
        for(int i=0;i<searchHistoryList.size();i++){
            if(operator=="SM1"||operator=="SM2"){
                //管理员可查看所有
                searchHistoryList1.add(searchHistoryList.get(i));
            }else{
                /*管理员只可查看自己*/
                if(searchHistoryList.get(i).getOperator().equals(operator)){
                    searchHistoryList1.add(searchHistoryList.get(i));
                }
            }

        }
        return list2PageUtil.listConvertToPage(searchHistoryList1,pageable);
    }


}
